﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace SPDevExplorer
{
    public partial class SiteExplorer
    {
        public static TreeNode CreateFile(TreeNode oParent, string fileName, FolderProperties fp)
        {
            string sXML = DevServices(oParent).CreateFile(fp.ID, fileName);

            if (!string.IsNullOrEmpty(sXML))
            {
                XmlDocument oDoc = new XmlDocument();
                oDoc.LoadXml(sXML);
                XmlElement oImported = (XmlElement) fp.Xml.OwnerDocument.ImportNode(oDoc.DocumentElement, true);
                fp.Xml.AppendChild(oImported);

                TreeNode oNew = new TreeNode(fileName, (int) NodeImages.File, (int) NodeImages.File);
                oParent.Nodes.Add(oNew);
                oNew.Tag = oImported;
                ConfigureTreeNode(oNew);
                oParent.TreeView.SelectedNode = oNew;
                oNew.ContextMenuStrip = ((SiteExplorer) oParent.TreeView.Parent).ctxMenuFile;
                return oNew;
            }

            return null;
        }

        private void DeleteFile(TreeNode fileNode)
        {
            FileProperties p = new FileProperties(fileNode);
            DevServices(fileNode).DeleteFile(p.Url);
        }

        private void CheckoutFile(TreeNode fileNode)
        {
            FileProperties p = new FileProperties(fileNode);

            if (!p.HasCheckedOut)
            {
                // Checkout file at server side, replace filenode XML with new info
                string sXML = DevServices(fileNode).CheckoutFile(p.Url);
                ReplaceFileNode(p, sXML);

                //string sFile = DevServices(_ctlExplorerTree.SelectedNode).GetFile(p.Url);
                string sPath = FileNodeToWorkingFolderFilePath(fileNode);

                // Possible situations:
                // DateTime(SharePointFile) > DateTime(WorkingFolderFile) => warning for overwrite, WorkingFolderFile is newer
                // DateTime(SharePointFile) <= DateTime(WorkingFolderFile) ==> overwrite
                // DateTime lastModificationDateFileInWorkingFolder = null;
                // DateTime lastModificationDateFileInSharePoint = p.TimeLastModifiedInSharePoint;

                if (File.Exists(sPath))
                {
                    FileInfo fi = new FileInfo(sPath);
                    fi.IsReadOnly = false;
                }
            }
        }

        private void EnsureFilesInFolder(TreeNode folderNode, bool bOverwrite)
        {
            foreach (TreeNode n in folderNode.Nodes)
            {
                FileProperties p = new FileProperties(n);
                if (p.Xml.Name == "File")
                {
                    string sPath = EnsurePath(n.Parent) + p.Name;
                    if (bOverwrite || !File.Exists(sPath))
                        DevServices(folderNode).GetFileBytesAsync(p.Url, n);
                }
            }
        }
    }
}